/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var map;
var services;
var infoWindow;
var placesList;

function initialize(){
    var trheim = new google.maps.LatLng(63.4297,10.3933);
    var genova = new google.maps.LatLng(44.4111, 8.9328);
    var larvik = new google.maps.LatLng(59.0811, 10.0164);
    var mosjoen = new google.maps.LatLng(65.8369, 13.1934);
    var hafjell = new google.maps.LatLng(61.2422, 10.4419);
    
    map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: hafjell,
        zoom: 15
    });
    
    var request = {
        location: hafjell,
        radius: 1000,
        types: ['bar','Pub', 'Bar', 'pub', 'discotheque', 'Discotheque', 'night_club', 'Night_Club', 'Night_club', 'nightclub', 'Nightclub', 'lounge', 'Lounge']
        //query: 'bar OR pub OR discotheque'
    };
    
    service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, callback);
}

function callback(results, status, pagination){
    if(status === google.maps.places.PlacesServiceStatus.OK){
        console.log("results.length");
        console.log(results.length);
        for(var i=0; i<results.length; i++){
            var place = results[i];
            console.log(place);
            //Create markers
            new google.maps.Marker({
                map: map,
                position: place.geometry.location,
                title: place.name
                //icon: markerIcon
            });
        }
        //pagination
        if(pagination.hasNextPage){
            var moreButton = document.getElementById('more');
            
            moreButton.disabled  = false;
            
            google.maps.event.addDomListenerOnce(moreButton, 'click',
                function(){
                    moreButton.disabled = true;
                    pagination.nextPage();
                });
        }
    }
}

function createMarkers(places){
    var bounds = new google.maps.LatLngBounds();
    
    for(var i=0, place; place = places[i]; i++){
        var image = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
        }
        
        var marker = new google.maps.Marker({
            map: map,
            icon: image,
            title: place.name || "Name",
            position: place.geometry.location
        });
        
        placesList.innerHTML += '<li>' + place.name + '<li>';
        
        bounds.extend(place.geometry.location);
    }
    map.fitBounds(bounds);
}

google.maps.event.addDomListener(window, 'load', initialize);